from hep.aida import *

from java.awt import Color
from java.awt import Font
from java.lang import *
from java.util import *
from java.io import *

from jhplot.jadraw import *
from jhplot  import *
from jplot  import *

from fr.in2p3.ipnl.aidautilities import *
from fr.in2p3.ipnl.oflap.scint import *
from jscinttools.utilities.jhplotutilities import *
from jscinttools.scint.models import *

Locale.setDefault(Locale.ENGLISH)

showFigures = True
writeFigures = not showFigures
"""
k1_us = 123.9
k2_us = 2.5
d_K = 69.3
capK_us = 0.0067
dE_K = 1229.0
n1 = 1.0
#kag1_us = 354.6
#kag2_us = 1.21
#dag_K = 103.3
kag1_us = 355.6
kag2_us = 5.3
dag_K = 76.7

n3 = 0.1
"""

"""
k1_us = 123.9
k2_us = 2.5
d_K = 69.3
capK_us = 0.0067
dE_K = 1229.0
n1 = 1.0
#kag1_us = 354.6
#kag2_us = 1.21
#dag_K = 103.3
#kag1_us = 355.6
#kag2_us = 5.3
#dag_K = 76.7
kag1_us = 356.0
kag2_us = 9.8
dag_K = 66.4
n3 = 0.51
"""
#norm = 3.87
#n3Rel = 0.51

#norm = 3.691
#n3Rel = 0.0987

""" 3 lvl AG
k1_us = 123.9
k2_us = 2.5
d_K = 69.3
capK_us = 0.0067
dE_K = 1229.0
#n1 = 1.0
n1 = 9
#kag1_us = 354.6
#kag2_us = 1.21
#dag_K = 103.3
kag1_us = 356.0
#kag2_us = 0.1
kag2_us = 9.58
dag_K = 66.4
capKag_us = 0.000019
deltaEag_K = 3272.3
#n3 = 0.1318
n3 = 1.5
"""

""" 2 lvl AG """
k1_us = 123.9
k2_us = 2.5
d_K = 69.3
capK_us = 0.0067
dE_K = 1229.0
#n1 = 1.0
n1 = 7
#kag1_us = 354.6
#kag2_us = 1.21
#dag_K = 103.3
kag1_us = 356.0
#kag2_us = 0.1
kag2_us = 0.1
dag_K = 150
#n3 = 0.1318
n3 = 0.5


""" 1 lvl AG
k1_us = 123.9
k2_us = 2.5
d_K = 69.3
capK_us = 0.0067
dE_K = 1229.0
#n1 = 1.0
n1 = 9
#kag1_us = 354.6
#kag2_us = 1.21
#dag_K = 103.3
kag1_us = 356.0
#kag2_us = 0.1
#kag2_us = 9.58
#dag_K = 66.4
#capKag_us = 0.000019
#deltaEag_K = 3272.3
#n3 = 0.1318
n3 = 1.5
"""

minT = 1.0
maxT = 1000.0

""" Define colors and fonts"""
figType = [".png", ".eps"]
colors = [Color.blue, Color.red, Color(0, 170, 0), Color.black, Color.yellow]
axisLabelFont = Font(Font.SERIF, Font.PLAIN, 25)
titleFont = Font(Font.SERIF, Font.PLAIN, 25)
legendFont = Font(Font.SERIF, Font.PLAIN, 20)
ticFont = Font(Font.SERIF, Font.PLAIN, 20)
statBoxFont = Font(Font.SERIF, Font.PLAIN, 20)


""" Define the Path and files names"""
outPath = "/home/marc-antoine/Dropbox/BGO"

temper = ScintUtil.logFill(minT,maxT)

#model = ThreeLvlNonRadTrap(1.0/k1_us, 1.0/k2_us, d_K, 1.0/capK_us, dE_K, n1, 1.0/kag_us, n3)
#model = ThreeLvlNonRadTrap(k1_us, k2_us, d_K, capK_us, dE_K, n1, kag1_us, n3)

#model = ThreeLvlNonRadTrap2Terms(1.0/k1_us, 1.0/k2_us, d_K, 1.0/capK_us, dE_K, n1, 1.0/kag1_us, 1.0/kag2_us, dag_K, n3)
model = ThreeLvlNonRadTrap2Terms(k1_us, k2_us, d_K, capK_us, dE_K, n1, kag1_us, kag2_us, dag_K, n3)

#model = ThreeLvlNonRadTrap3Terms(k1_us, k2_us, d_K, capK_us, dE_K, n1, kag1_us, kag2_us, dag_K, capKag_us, deltaEag_K, n3)

tau_us = []
tauAg_us = []
lyMain = []
lyAg = []
lyTot = []

for i in range(len(temper)):
	param = CalcParameters(model, temper[i])
	tau_us.append(param.tau_us)
	tauAg_us.append(param.tauAg_us)
	lyMain.append(param.ly)
	lyAg.append(param.lyAg)
	lyTot.append(param.lyTot)
	

"""
###############################################################
# Plotting Part
###############################################################
"""
""" Create the plottable JHep objects from AIDA IObjects"""
tau =P1D("tau", temper, tau_us)
tau.setSymbol(4)
tau.setSymbolSize(4)
tau.color = colors[0]
tau.errAll = False

tauAg =P1D("tauAg", temper, tauAg_us)
tauAg.setSymbol(4)
tauAg.setSymbolSize(4)
tauAg.color = colors[1]
tauAg.errAll = False

ly =P1D("LY", temper, lyMain)
ly.setSymbol(4)
ly.setSymbolSize(4)
ly.color = colors[0]
ly.errAll = False

lyAg =P1D("LYag", temper, lyAg)
lyAg.setSymbol(4)
lyAg.setSymbolSize(4)
lyAg.color = colors[1]
lyAg.errAll = False

lyT =P1D("LYtot", temper, lyTot)
lyT.setSymbol(4)
lyT.setSymbolSize(4)
lyT.color = colors[2]
lyT.errAll = False


""" Create the Keys for the Legend """
lyKey = JaKey(ly.symbol, "Main", 0.20, 0.70, "NDC")
lyKey.color = ly.color
lyKey.keyColor = ly.color
lyKey.penWidth = 2
lyKey.font = legendFont

lyAgKey = JaKey(lyAg.symbol, "Afterglow", 0.20, 0.75, "NDC")
lyAgKey.color = lyAg.color
lyAgKey.keyColor = lyAg.color
lyAgKey.penWidth = 2
lyAgKey.font = legendFont

lyTKey = JaKey(lyT.symbol, "LY Total", 0.20, 0.80, "NDC")
lyTKey.color = lyT.color
lyTKey.keyColor = lyT.color
lyTKey.penWidth = 2
lyTKey.font = legendFont


""" Create the Plotter for the tau"""
tauPlotter = HPlotJa("tau", 600, 600, 1, 1, True)
tauPlotter.setGTitle("Scint Model tau", titleFont)
tauPlotter.antiAlias = False
if showFigures:
	tauPlotter.visible(True)
else:
	tauPlotter.visible(False)

tauPlotter.setPad(1, 1, 0.125, 0.125, 0.75, 0.75)
tauPlotter.setRange(1, 1000, 0.1, 1000)
tauPlotter.setLogScale(0, True)
tauPlotter.setLogScale(1, True)
tauPlotter.ticFont = ticFont
tauPlotter.showStatBox = False
tauPlotter.showKey(False)
tauPlotter.nameX = "Temperature (K)"
tauPlotter.nameY = "Decay Time (&micro;s)"

tauPlotter.draw(tau)
tauPlotter.draw(tauAg)
tauPlotter.draw(lyKey)
tauPlotter.draw(lyAgKey)


	
#tauPlotter.draw(shKeyQ)
#tauPlotter.draw(mnKeyQ)
#tauPlotter.draw(lgKeyQ)
#tauPlotter.draw(txt)
	
""" Modifies the JaAxes and the axes labels"""
labX_tau = tauPlotter.getLabelX()[0][0]
labX_tau.font = axisLabelFont
HPlotJaUtil.setLabelLocationAtCenter(0, labX_tau, tauPlotter)
labY_tau = tauPlotter.getLabelY()[0][0]
labY_tau.font = axisLabelFont
HPlotJaUtil.setLabelLocationAtCenter(1, labY_tau, tauPlotter, -0.01)

theAxes_tau = tauPlotter.getPad()
theAxes_tau.setLabelSpace(0, 0.0)
theAxes_tau.setLabelSpace(1, -0.025)
HPlotJaUtil.setTickLabelOneToPower(0, theAxes_tau)
HPlotJaUtil.setTickLabelOneToPower(1, theAxes_tau)

tauPlotter.update()

""" Saves or not the plot"""
if writeFigures:
	for i in range(len(figType)):
		outFileName = "tauModel1lvlAg" + figType[i]
		tauPlotter.export(outPath + File.separator + outFileName)
		print("Writting File : " + outPath + File.separator + outFileName)
		
tauPlotter.update()



""" Create the Plotter for the LY"""
lyPlotter = HPlotJa("LY", 600, 600, 1, 1, True)
lyPlotter.setGTitle("Scint Model LY", titleFont)
lyPlotter.antiAlias = False
if showFigures:
	lyPlotter.visible(True)
else:
	lyPlotter.visible(False)

lyPlotter.setPad(1, 1, 0.125, 0.125, 0.75, 0.75)
lyPlotter.setRange(1, 1000, 0.0, 10)
lyPlotter.setLogScale(0, True)
lyPlotter.setLogScale(1, False)
lyPlotter.ticFont = ticFont
lyPlotter.showStatBox = False
lyPlotter.showKey(False)
lyPlotter.nameX = "Temperature (K)"
lyPlotter.nameY = "LY"

lyPlotter.draw(ly)
lyPlotter.draw(lyAg)
lyPlotter.draw(lyT)
lyPlotter.draw(lyKey)
lyPlotter.draw(lyAgKey)
lyPlotter.draw(lyTKey)

	
#lyPlotter.draw(shKeyQ)
#lyPlotter.draw(mnKeyQ)
#lyPlotter.draw(lgKeyQ)
#lyPlotter.draw(txt)
	
""" Modifies the JaAxes and the axes labels"""
labX_ly = lyPlotter.getLabelX()[0][0]
labX_ly.font = axisLabelFont
HPlotJaUtil.setLabelLocationAtCenter(0, labX_ly, lyPlotter)
labY_ly = lyPlotter.getLabelY()[0][0]
labY_ly.font = axisLabelFont
HPlotJaUtil.setLabelLocationAtCenter(1, labY_ly, lyPlotter, -0.01)

theAxes_ly = lyPlotter.getPad()
theAxes_ly.setLabelSpace(0, 0.0)
#theAxes_ly.setLabelSpace(1, -0.025)
theAxes_ly.setLabelSpace(1, 0.0)
HPlotJaUtil.setTickLabelOneToPower(0, theAxes_ly)
HPlotJaUtil.setTickLabelOneToPower(1, theAxes_ly)

lyPlotter.update()

""" Saves or not the plot"""
if writeFigures:
	for i in range(len(figType)):
		outFileName = "lyModel1lvlAg" + figType[i]
		lyPlotter.export(outPath + File.separator + outFileName)
		print("Writting File : " + outPath + File.separator + outFileName)
		
lyPlotter.update()

print("That's all folks !")

